﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ODP.Model;
using ODP.Helper;

namespace ODP.Web
{
    /// <summary>
    /// FileUpload 的摘要说明
    /// </summary>
    public class FileUpload : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.QueryString["action"];

            if (!string.IsNullOrEmpty(action))
            {
                action = action.ToLower();
            }

            switch (action)
            {
                case "uploadpic":
                    UploadPic(context);
                    break;
            }
        }

        private void UploadPic(HttpContext context)
        {
            string fileName = "";
            string result = new FileUploadHelper().UploadFile(context.Request.Files, "UploadFiles\\",ref fileName);

            result = result.Replace("\\", "\\\\");

            if (!string.IsNullOrEmpty(result))
            {
                result = string.Format("{{isSuccess:\"True\",message:\"上传成功!\",fileName:\"{0}\",fileFullName:\"{1}\",filePath:\"{2}\"}}", fileName, string.Format("../{0}/{1}", "UploadFiles", fileName), result);
            }
            else
            {
                result = string.Format("{{isSuccess:\"False\",message:\"上传失败!\",fileName:\"{0}\",fileFullName:\"{1}\",filePath:\"{2}\"}}", fileName, "","");
            }
            context.Response.Clear();
            context.Response.Write(result);
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}